<?php

namespace App\Containers\AppSection\User\UI\CLI\Commands;

use App\Containers\AppSection\Authorization\Tasks\GetAllRolesTask;
use App\Containers\AppSection\User\Actions\RegisterInnerUserAction;
use App\Containers\AppSection\User\UI\CLI\Requests\RegisterInnerUserRequest;
use App\Ship\Parents\Commands\ConsoleCommand;
use DB;
use Validator;

class CreateInnerUserCommand extends ConsoleCommand
{
    protected $signature = 'create:innerUser';

    protected $description = 'Create a new Inner User';

    public function handle(): void
    {
        $auto = $this->confirm('Do you want to generate user?');

        $roles = app(GetAllRolesTask::class)->withType('inner')->run(true)->pluck('name')->toArray();

        if (empty($roles)) {
            $this->error('Inner roles missing');
            return;
        }

        if ($auto) {
            $innerRoles = app(GetAllRolesTask::class)->withType('inner')->vacant()->run(true);
            $vacantRoles = array_intersect($roles, $innerRoles->pluck('name')->toArray());
        }

        if (empty($vacantRoles)) {
            $auto = false;
            $this->warn('There are no free roles to generate! Please, enter inner user manually!');
        } else {
            $roles = $vacantRoles;
        }

        $role = $this->choice('Enter the role for this user', $roles);

        $fio = $auto ? ucfirst($role) : $this->ask('Enter the fio for this user');
        $email = $auto ? $role . '@incrowd.ru' : $this->ask('Enter the email address of this user');

        $phone = $auto ? $this->generatePhone() : $this->ask('Enter the phone for this user! +7(___)___-__-__');

        $password = $auto ? $role . 'Incrowd' : $this->secret('Enter the password for this user');
        $password_confirmation = $auto ? $password : $this->secret('Please confirm the password');

        if ($password !== $password_confirmation) {
            $this->error('Passwords do not match - exiting!');
            return;
        }

        $request = new RegisterInnerUserRequest([
            'fio' => $fio,
            'email' => $email,
            'password' => $password,
            'roles' => [$role],
            'phone' => $phone,
        ]);

        $validator = Validator::make($request->all(), $request->rules());
        if ($validator->fails()) {
            $this->info('Inner User not created. See error messages below:');

            foreach ($validator->errors()->all() as $error) {
                $this->error($error);
            }
            return;
        }

        DB::transaction(function () use ($request) {
            app(RegisterInnerUserAction::class)->run($request);
        });

        $this->info('New user - "' . ucfirst($fio) . '" with email - "' . $email . '" was successfully created');

    }

    private function generatePhone(): string
    {
        return '+7(9' . generateRandomInt(2) . ')' .
            generateRandomInt(3) . '-' .
            generateRandomInt(2) . '-' .
            generateRandomInt(2);
    }
}
